package com.lokyiu.mybible.db.sqlite;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lokyiu.mybible.dao.VerseDao;
import com.lokyiu.mybible.db.sqlite.def.BookColumns;
import com.lokyiu.mybible.db.sqlite.def.VerseColumns;
import com.lokyiu.mybible.domain.BibleBook;
import com.lokyiu.mybible.domain.Verse;

public class VerseDaoImpl implements VerseDao {
	private SQLiteDatabase db;
	
	public VerseDaoImpl(SQLiteDatabase db){
		this.db = db;
	}
	
	public List<Verse> getVerses() {
		List<Verse> verses = new ArrayList<Verse>();
		Cursor cursor = db.query(VerseColumns.TABLENAME, new String[] {VerseColumns.VERSES_BIBLEBOOK,VerseColumns.VERSES_CHPATER_NUMBER,VerseColumns.VERSES_NUMBER,VerseColumns.VERSES_CONTENT},
								null, null, null, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				Verse verse = new Verse(cursor.getString(0),Integer.parseInt(cursor.getString(1)),Integer.parseInt(cursor.getString(2)),cursor.getString(3));
				verses.add(verse);
			} while (cursor.moveToNext());
		}
		cursor.close();
		return verses;
	}
	
	public void saveVerse(Verse verse){
		ContentValues values = new ContentValues();
		values.put(VerseColumns.VERSES_BIBLEBOOK, verse.getBibleBook());
		values.put(VerseColumns.VERSES_CHPATER_NUMBER, verse.getChapter());
		values.put(VerseColumns.VERSES_NUMBER, verse.getVerse());
		values.put(VerseColumns.VERSES_CONTENT, verse.getContent());
		db.insert(VerseColumns.TABLENAME, null, values);
	}
	
	public void saveVerses(List<Verse> verses){
		db.beginTransaction();
		for(Verse verse : verses){
			saveVerse(verse);
		}
		db.setTransactionSuccessful();
		db.endTransaction();
	}

}
